package view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class Student_Course extends Component implements ActionListener {
    ActionEvent event;
    private Connection connection;
    private String id;
    private int selectclass;
    public JFrame course=new JFrame();
    private JPanel panel = new JPanel(new GridLayout());
    public Student_Course(Connection connection,String id){
        this.connection=connection;
        this.id=id;
        init();
    }

    void init(){
        course=new JFrame();
        course.setTitle("学生选课查询");
        course.setSize(500,500);
        JRadioButton b1=new JRadioButton("大数据技术");
        b1.setFont(new Font("", Font.BOLD, 15));
        JRadioButton b2=new JRadioButton("算法分析与设计");
        b2.setFont(new Font("", Font.BOLD, 15));
        JRadioButton b3=new JRadioButton("Kotlin语言");
        b3.setFont(new Font("", Font.BOLD, 15));
        JRadioButton b4=new JRadioButton("汇编语言");
        b4.setFont(new Font("", Font.BOLD, 15));
        b1.setBounds(75,50,100,30);
        b2.setBounds(75,90,100,30);
        b3.setBounds(75,130,100,30);
        b4.setBounds(75,170,100,30);
        ButtonGroup bg=new ButtonGroup();
        JTextArea area=new JTextArea("请选择你的选课,点击选择后页面会自动退出，请注意",40,40);
        area.setFont(new Font("", Font.BOLD, 22));
        area.setEditable(false);
        area.setLineWrap(true);
        bg.add(b1);
        bg.add(b2);
        bg.add(b3);
        bg.add(b4);
        panel.add(area);
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        course.add(panel);
        course.setLocationRelativeTo(null);
        course.setVisible(true);
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(b1.isSelected())
                {
                    JOptionPane.showMessageDialog(course,"你已选择大数据技术课程成功");
                    selectclass=1;
                    updateCourse(selectclass);
                    course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );
                }
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(b2.isSelected())
                {
                    JOptionPane.showMessageDialog(course,"你已选择算法设计与分析课程成功");
                    selectclass=2;
                    updateCourse(selectclass);
                    course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );
                }
            }
        });
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(b3.isSelected())
                {
                    JOptionPane.showMessageDialog(course,"你已选择Kotlin语言课程成功");
                    selectclass=3;
                    updateCourse(selectclass);
                    course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );
                }
            }
        });
        b4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(b4.isSelected())
                {
                    JOptionPane.showMessageDialog(course,"你已选择汇编语言成功");
                    selectclass=4;
                    updateCourse(selectclass);
                    course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );
                }
            }
        });

    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
    public void updateCourse(int i){
        String a="大数据技术";
        String b="算法分析与设计";
        String c="Kotlin语言";
        String d="汇编语言";
        switch (i){
            case 1:
                try{
                    String sql = "update  学生基本信息表 set course=? where id=?";
                    PreparedStatement pstmt = connection.prepareStatement(sql);
                    pstmt.setString(1,a);
                    pstmt.setString(2,id);
                    pstmt.executeUpdate();
                }catch (Exception abc){
                }
                break;
            case 2:
                try{
                    String sql = "update  学生基本信息表 set course=? where id=?";
                    PreparedStatement pstmt = connection.prepareStatement(sql);
                    pstmt.setString(1,b);
                    pstmt.setString(2,id);
                    pstmt.executeUpdate();
                }catch (Exception abc){
                }
                break;
            case 3:
                try{
                    String sql = "update  学生基本信息表 set course=? where id=?";
                    PreparedStatement pstmt = connection.prepareStatement(sql);
                    pstmt.setString(1,c);
                    pstmt.setString(2,id);
                    pstmt.executeUpdate();
                }catch (Exception abc){
                }
                break;
            case 4:
                try{
                    String sql = "update  学生基本信息表 set course=? where id=?";
                    PreparedStatement pstmt = connection.prepareStatement(sql);
                    pstmt.setString(1,d);
                    pstmt.setString(2,id);
                    pstmt.executeUpdate();
                }catch (Exception abc){
                }
                break;
        }

    }
}